package pl.edu.pk.mech.kkbus.model;

import static pl.edu.pk.mech.kkbus.staticstrings.UserProperty.USER_EMAIL;
import static pl.edu.pk.mech.kkbus.staticstrings.UserProperty.USER_ENABLED;
import static pl.edu.pk.mech.kkbus.staticstrings.UserProperty.USER_NAME;
import static pl.edu.pk.mech.kkbus.staticstrings.UserProperty.USER_PASS;
import static pl.edu.pk.mech.kkbus.staticstrings.UserProperty.USER_PHONE;
import static pl.edu.pk.mech.kkbus.staticstrings.UserProperty.USER_SURNAME;
import static pl.edu.pk.mech.kkbus.staticstrings.UserProperty.USER_AUTHORITIES;
import static pl.edu.pk.mech.kkbus.staticstrings.UserProperty.USER_HASH;
import static pl.edu.pk.mech.kkbus.staticstrings.UserProperty.USER_POINTS;
import static pl.edu.pk.mech.kkbus.staticstrings.UserProperty.USER_ROLES_DIVIDER;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

import org.springframework.security.core.GrantedAuthority;

import com.google.appengine.api.datastore.Entity;

public class User extends Pojo implements Serializable {

	private static final long serialVersionUID = 2652062810549585286L;

	public enum Role implements GrantedAuthority {
        ADMIN (0),
        USER (1),
        WORKER(2);

        private int bit;

        Role(int bit) {
            this.bit = bit;
        }

        @Override
        public String getAuthority() {
            return toString();
        }
    }
    
    private long id;
    @NotNull
    @Size(min = 3, max = 20)
    private String name;
    @NotNull
    @Size(min = 3, max = 20)
    private String surname;
    @NotNull
    private Date birthday;
    @NotNull
    @Pattern(regexp=".+@.+\\.[a-z]+")
    private String email;
    @NotNull
    @Size(min = 9, max = 11)
    private String phone;
    private boolean enabled;
    private Set<Role> authorities;
    @NotNull
    @Size(min = 3, max = 20)
    private String password;
    private String hash;
    private int points;

    public User() {        
    }
    
    public User(Entity entity){
        this.id = entity.getKey().getId();
        this.email = (String) entity.getProperty(USER_EMAIL);
        this.name = (String) entity.getProperty(USER_NAME);
        this.password = (String) entity.getProperty(USER_PASS);
        this.surname = (String) entity.getProperty(USER_SURNAME);
        
        this.authorities = new HashSet<Role>();
        String authorities = (String)entity.getProperty(USER_AUTHORITIES);
        for(String role : authorities.split(USER_ROLES_DIVIDER)) {
            this.authorities.add(Role.valueOf(role));
        }

        this.hash = (String) entity.getProperty(USER_HASH);
        this.enabled = Boolean.valueOf(entity.getProperty(USER_ENABLED).toString());
        this.phone = (String) entity.getProperty(USER_PHONE);
        this.points = Integer.valueOf(entity.getProperty(USER_POINTS).toString());
    }
    
    public int getPoints() {
		return points;
	}

	public void setPoints(int points) {
		this.points = points;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSurname() {
		return surname;
	}

	public void setSurname(String surname) {
		this.surname = surname;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public boolean isEnabled() {
		return enabled;
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public Set<Role> getAuthorities() {
		return authorities;
	}

	public void setAuthorities(Set<Role> authorities) {
		this.authorities = authorities;
	}

        public String getHash() {
            return hash;
        }

        public void setHash(String hash) {
            this.hash = hash;
        }      

	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", surname=" + surname
				+ ", birthday=" + birthday + ", email=" + email + ", phone="
				+ phone + ", enabled=" + enabled + ", authorities="
				+ authorities + ", password=" + password + "]";
	}

}